package com.example.study.controller;

import com.alibaba.fastjson.JSON;
import com.example.study.annotation.UnInterception;
import com.example.study.entity.User;
import com.example.study.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("redis")
public class TestRedisController {
    private static final Logger logger = LoggerFactory.getLogger(TestRedisController.class);
    @Resource
    private RedisService redisService;
    @UnInterception
    @RequestMapping("test")
    public void getstring() {
        //测试redis的string类型
        redisService.setString("weichat","程序员私房菜");
        logger.info("我的微信公众号为：{}", redisService.getString("weichat"));

        // 如果是个实体，我们可以使用json工具转成json字符串，
        User user = new User(4, "张三1","123456");
        redisService.setString("userInfo", JSON.toJSONString(user));
        logger.info("用户信息：{}", redisService.getString("userInfo"));
    }
    @UnInterception
    @RequestMapping("hash")
    public void hash() {
        // 如果是个实体，我们可以使用json工具转成json字符串，
        User user = new User(4, "张三1","123456");
        redisService.setString("userInfo", JSON.toJSONString(user));
        //测试redis的hash类型
        redisService.setHash("user", "name", JSON.toJSONString(user));
        logger.info("用户姓名：{}", redisService.getHash("user","name"));
    }
    @UnInterception
    @RequestMapping("list")
    public void list() {
        //测试redis的list类型
        redisService.setList("list", "football");
        redisService.setList("list", "basketball");
        List<String> valList = redisService.getList("list",0,-1);
        for(String value :valList){
            logger.info("list中有：{}", value);
        }
    }
}
